<?php
namespace App\Service\Xcx;

class IndexService extends BaseService
{
    //用户微信信息更新
    public static function wxinfo($openid)
    {
        $res = self::dbConn(self::$dbWx)->where('openid', $openid)->first();
        $upData['openid'] = $openid;
        $upData['uptime'] = toDate();
        $backData = [
            'openid' => $openid,
            'nickName' => $res ? $res->nickName : '',
            'avatarUrl' => $res ? $res->avatarUrl : '',
        ];
        if ($res) {
            self::dbConn(self::$dbWx)->where('openid', $openid)->update($upData);
            $backData['uid'] = $res->id;
        } else {
            $upData['addtime'] = toDate();
            $uid = self::dbConn(self::$dbWx)->insertGetId($upData);
            $backData['uid'] = $uid;
        }
        return self::toback(0, '', $backData);
    }

    //用户微信信息更新
    public static function wxModify($uid, $ary)
    {
        $res = self::dbConn(self::$dbWx)->where('id', $uid)->first();
        $upData['uptime'] = toDate();
        if ($ary['nick']) $upData['nickName'] = $ary['nick'];
        if ($ary['head']) $upData['avatarUrl'] = $ary['head'];
        if (!$res) {
            return self::toback(-2, '没有记录');
        }
        self::dbConn(self::$dbWx)->where('id', $uid)->update($upData);
        return self::toback();
    }

    //用户微信头像
    public static function wxHead($uid, $wxhead)
    {
        $res = self::dbConn(self::$dbWx)->where('id', $uid)->first();
        $upData['uptime'] = toDate();
        if (!$res) {
            return self::toback(-2, '没有记录');
        }
        self::dbConn(self::$dbWx)->where('id', $uid)->update(['avatarUrl' => $wxhead]);
        return self::toback();
    }

    //我的页面
    public static function my($uid)
    {
        $arys = [];
        $arys['order_pay'] = self::dbConn(self::$dbMallOrder)
            ->where('del', 0)
            ->where('uid', $uid)
            ->where('status', 1)
            ->count();
        $arys['order_send'] = self::dbConn(self::$dbMallOrder)
            ->where('del', 0)
            ->where('uid', $uid)
            ->where('status', 2)
            ->count();
        $arys['order_accept'] = self::dbConn(self::$dbMallOrder)
            ->where('del', 0)
            ->where('uid', $uid)
            ->where('status', 3)
            ->count();
        $arys['order_finish'] = self::dbConn(self::$dbMallOrder)
            ->where('del', 0)
            ->where('uid', $uid)
            ->where('status', 5)
            ->count();
        $arys['address_num'] = self::dbConn(self::$dbAddress)
            ->where('del', 0)
            ->where('uid', $uid)
            ->count();
        return $arys;
    }

    //获取主菜单
    public static function bomMenus($type)
    {
        $res = self::dbConn(self::$dbXcxMenu)
            ->select('id', 'title', 'link', 'icon0', 'icon1')
            ->where('is_bom', 1)
            ->where('type', $type)
            ->get();
        if (!count($res)) return [];
        foreach ($res as &$v) {
            $v->icon0 = file_exists($v->icon0) ? env('DOMAIN') . '/'. $v->icon0 : '';
            $v->icon1 = file_exists($v->icon1) ? env('DOMAIN') . '/'. $v->icon1 : '';
        }
        return $res;
    }

    //获取模板
    public static function typeInit()
    {
        $ary['type'][] = ['title' => '商城', 'en' => 'mall', 'tmp_type' => 1,
            'child' => self::dbConn(self::$dbMallShop)
                ->select('id', 'shop_name as title')
                ->where('status', 0)->get(),
        ];
        $ary['type'][] = ['title' => '邀请函', 'en' => 'yqh', 'tmp_type' => 2,
            'child' => self::dbConn(self::$dbYqh)
                ->select('id', 'title')
                ->where('del', 0)->get(),
        ];
        //$ary['sel'] = ['tmp_type' => 2, 'id' => 1]; //tmp_type0不显示，1商城，2邀请函
       // $ary['sel'] = 1; //tmp_type0不显示，1用户定义
        $configOne = self::dbConn(self::$dbConfig)->where('id', 1)->first();
        $ary['sel'] = $configOne&&$configOne->val ? $configOne->val : 0;
        return $ary;
    }

    //首页轮播图
    public static function banner($type = 1)
    {
        $db = self::dbConn(self::$dbBanner)
            ->select('id', 'img')
            ->where('del', 0);
        if ($type) $db = $db->where('type', $type);
        $res = $db->get();
        if (!count($res)) return [];
        foreach ($res as &$v) {
            $v->img = file_exists($v->img) ? env('DOMAIN') . '/'. $v->img : '';
        }
        return $res;
    }
}